<?php
/**
 * --------------------------------------------------------------
 * admin模块函数库
 * Author: zd <kfzphp@126.com>
 * Date: 2018-06-01
 * Project: kfzphp.com
 * --------------------------------------------------------------
 */

/**
 * 获取用户角色权限获取用户跳转的URL
 * @param  [type]     $tree [description]
 * @return [type]           [description]
 */
function get_jumpurl_by_roletree($tree)
{
    if(empty($tree)) {
        return false;
    }
    // 系统菜单
    $menu = config('SYSTEM_MENU');
    // 用户权限
    $exp = explode(',', $tree);
    // 循环判断
    foreach ($exp as $k => $v) {
        if(is_numeric($v)) {
            // 主菜单跳转
            if((strlen($v) == 1) && isset($menu[$v]['controller']) && isset($menu[$v]['action'])) {
                return '/' . MODULE_NAME . '/' . $menu[$v]['controller'] . '/' . $menu[$v]['action'];
            }
            // 子菜单跳转
            if(strlen($v) == 2) {
                $m = substr($v, 0, 1);
                if(isset($menu[$m]['submenu'][$v]['controller']) && isset($menu[$m]['submenu'][$v]['action'])) {
                    return '/' . MODULE_NAME . '/' . $menu[$m]['submenu'][$v]['controller'] . '/' . $menu[$m]['submenu'][$v]['action'];
                }
            }
        }
    }
    return false;
}

/**
 * 判断用户是否有权限访问模块
 * @param  [type]     $tree [description]
 * @return [type]           [description]
 */
function show_roletree($tree)
{
    if($tree) {
        // 权限ID
        $exp = explode(',', $tree);
        // 获取当前URL的权限ID
        $menu = config('SYSTEM_MENU');
        foreach ($menu as $k => $v) {
            if(isset($v['submenu'])) {
                foreach ($v['submenu'] as $key => $val) {
                    if((CONTROLLER_NAME == $val['controller']) && (ACTION_NAME == $val['action'])) {
                        if(in_array($key, $exp)) {
                            return true;
                        }
                    }
                }
            } else {
                if((CONTROLLER_NAME == $v['controller']) && (ACTION_NAME == $v['action'])) {
                    if(in_array($k, $exp)) {
                        return true;
                    }
                }
            }
        }
        return false;
    } else {
        return false;
    }
}

/**
 * 生成系统菜单
 * @param  [type]     $tree [description]
 * @return [type]           [description]
 */
function get_system_menu($tree)
{
    // 开始获取菜单列表
    $menu = config('SYSTEM_MENU');
    $role = explode(',', $tree);
    // 增加主菜单ID
    $role = get_menu_tree($role);
    $html = '';
    foreach ($menu as $k => $v) {
        if(in_array($k, $role)) {
            // 判断是否有子菜单
            if(isset($v['submenu'])) {
                // 主菜单URL
                $menu_url = 'javascript:;';
                // 子菜单向下箭头
                $submenu_down = '<i class="icon-chevron-down"></i>';
                // 子菜单HTML
                $submenu_html = '">';
                // 获取主菜单K值
                $menu_key = '';
                foreach ($v['submenu'] as $key => $val) {
                    if(in_array($key, $role)) {
                        // 子菜单选中状态
                        if(CONTROLLER_NAME == $val['controller'] && ACTION_NAME == $val['action']) {
                            $submenu_class = 'style="opacity: 1; font-weight:bold;"';
                            $submenu_clicked = 'class="clicked"';
                            $menu_key = $k;
                        } else {
                            $submenu_class = '';
                            $submenu_clicked = '';
                        }
                        // 主菜单URL
                        $submenu_url = '/' . MODULE_NAME . '/' . $val['controller'] . '/' . $val['action'];
                        $submenu_html .= '<li><a ' . $submenu_clicked . ' href="' . $submenu_url . '" class="active" ' . $submenu_class . '>' . $val['name'] . '</a></li>';
                    }
                }
                $submenu_html .= '</ul>';
                // 主菜单选中状态
                if($k == $menu_key) {
                    $menu_display = '<ul class="active submenu showUl';
                    $menu_class = 'style="opacity: 1;"';
                    $menu_clicked = 'class="clicked"';
                    // 选中状态时箭头标志
                    $active_arrow = '<div class="pointer"><div class="arrow"></div><div class="arrow_border"></div></div>';
                } else {
                    $menu_display = '<ul class="active submenu';
                    $menu_class = '';
                    $menu_clicked = '';
                    $active_arrow = '';
                }
            } else {
                $submenu_down = '';     // 向下箭头标志
                $menu_display = '';     // 子菜单
                $submenu_html = '';     // 子菜单内容
                // 主菜单URL
                $menu_url = '/' . MODULE_NAME . '/' . $v['controller'] . '/' . $v['action'];
                // 主菜单选中状态
                if(CONTROLLER_NAME == $v['controller'] && ACTION_NAME == $v['action']) {
                    $menu_class = 'style="opacity: 1;"';
                    $menu_clicked = 'class="clicked"';
                    // 选中状态时箭头标志
                    $active_arrow = '<div class="pointer"><div class="arrow"></div><div class="arrow_border"></div></div>';
                } else {
                    $menu_class = '';
                    $menu_clicked = '';
                    $active_arrow = '';
                }
            }
            // 主菜单HTML
            $html .= '<li class="active">';
            $html .= $active_arrow;
            $html .= '<a ' . $menu_clicked . ' href="' . $menu_url . '" ' . $menu_class . '>';
            $html .= '<i class="' . $v['icon'] . '"></i>';
            $html .= '<span>' . $v['name'] . '</span>';
            $html .= $submenu_down;
            $html .= '</a>';
            $html .= $menu_display . $submenu_html;
            $html .= '</li>';
        }
    }
    return $html;
}

/**
 * 对数组重新设置
 * @param  [type]     $role [description]
 * @return [type]           [description]
 */
function get_menu_tree($role)
{
    $array = array();
    foreach ($role as $k => $v) {
        if(is_numeric($v)) {
            if(strlen($v) == 2) {
                $m = substr($v, 0, 1);
                if(!in_array($m, $role)) {
                    $array[] = $m;
                }
            }
            $array[] = $v;
        }
    }
    $array = array_unique($array);
    // 权限排序
    sort($array);
    return $array;
}

/**
 * dhtmlxtree转xml
 * @return [type]     [description]
 */
function dhtmlxtree_to_xml($title = '', $tree = array())
{
    if($tree) {
        header("Content-type:text/xml");
        $xml = '<?xml version="1.0" encoding="UTF-8"?>';
        $xml .= '<tree id="0">';
            $xml .= '<item text="' . $title . '" id="all" open="1">';
                foreach ($tree as $k => $v) {
                    $xml .= '<item text="' . $v['name'] . '" id="' . $v['id'] . '" checked="' . $v['checked'] . '"></item>';
                }
            $xml .= '</item>';
        $xml .= '</tree>';
        echo $xml;exit;
    } else {
        return false;
    }
}
